package com.example.design.b.flyweight;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 享元工厂
 */
public class ShapeFactory {
    private Map<String, Shape> shapes = new HashMap<>();
    private Random random = new Random();

    public Shape getCircle(String color) {
        Circle circle = (Circle) shapes.get(color);
        if (circle == null) {
            circle = new Circle();
            circle.setColor(color);
            shapes.put(color, circle);
        }
        circle.setRadius(randomRadius());
        circle.setX(randomX());
        circle.setY(randomY());
        return circle;
    }

    public int randomRadius() {
        return random.nextInt(100) + 1;
    }

    public int randomX() {
        return random.nextInt(10) + 1;
    }

    public int randomY() {
        return random.nextInt(10) + 1;
    }
}
